﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

public partial class PlayersHigh : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        sel_machine.DataSource = HighScoreMachines.LoadHighScoreMachines();
        sel_machine.DataTextField = "MachineName";
        sel_machine.DataValueField = "Machine";
        sel_machine.DataBind();

        sel_machine.Items.Insert(0, "Select...");
        sel_machine.Items[0].Value = "0";
        AjaxPro.Utility.RegisterTypeForAjax(typeof(PlayersHigh));
      
        
    }
    [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.Read)]
    public bool CreatePlayer(string p_Name, string Score, bool EnvioEmail, int Machine, bool WPPR,string email, string Player1, string Player2)
    {
        try
        {

            string StrPlayer1 = Player1.Trim();
            string StrPlayer2 = Player2.Trim();

            int PlayerNumber1;
            int PlayerNumber2;

            bool ArePlayer1 = int.TryParse(StrPlayer1, out PlayerNumber1);
            bool ArePlayer2 = int.TryParse(StrPlayer2, out PlayerNumber2);

            string PlayerNick1 = string.Empty;
            string PlayerNick2 = string.Empty; 

            if(ArePlayer1){
                Player _Player1 = Player.GetPlayerByNumber(PlayerNumber1);
                if(Player1 != null){
                    PlayerNick1 = _Player1.Player_Nick + " (" + _Player1.Player_Number + ")"; 
                } 
            }

            if(ArePlayer2){
                Player _Player2 = Player.GetPlayerByNumber(PlayerNumber2);
                if(Player2 != null){
                    PlayerNick2 = _Player2.Player_Nick + " (" + _Player2.Player_Number + ")"; 
                } 
            }

            PlayerHighScore oPlayer = null;
            string _nameNick = string.Empty; 
            if (!ArePlayer1 && !ArePlayer2)
            {
                oPlayer = new PlayerHighScore(p_Name, Machine, long.Parse(Score, System.Globalization.CultureInfo.InvariantCulture), WPPR, email, EnvioEmail);
            }
            else
            {              

                if (ArePlayer1)
                {
                    _nameNick = PlayerNick1;
                }

                if (ArePlayer2)
                {
                    _nameNick += " - " + PlayerNick2;
                }
                oPlayer = new PlayerHighScore(_nameNick, Machine, long.Parse(Score, System.Globalization.CultureInfo.InvariantCulture), WPPR, email, EnvioEmail);

            }

            if (oPlayer != null)
            {
                return PlayerHighScore.CreatePlayer(oPlayer);
            }
            else
            {
                return false;
            }
            
        }
        catch{
            return false;
        }
    }
   
}
